<template>
  <div
    style="border:1px solid #C0C0C0; border-radius: 4px; width: 500px; height: 400px;margin: 0 auto"
  >
    <h1>登录</h1>

    <el-form :model="storekeeper" :rules="rules" ref="storekeeper" label-width="180px" @keyup.enter.native="submitForm('storekeeper')">
      <el-form-item label="商家名称" prop="storekeeperName">
        <el-input v-model="storekeeper.storekeeperName"></el-input>
      </el-form-item>

      <el-form-item label="密码" prop="password">
        <el-input type="password" v-model="storekeeper.password"></el-input>
      </el-form-item>
      <el-button type="primary" plain v-on:click="change()">注册</el-button>
      <el-button v-on:click="resetForm('storekeeper')">重置</el-button>
      <el-button type="primary" v-on:click="submitForm('storekeeper')">登录</el-button>
    </el-form>
    <br />
    <el-button type="warning" @click="toStoreLogin()" size="mini">用户登录/注册</el-button>
  </div>
</template>

<script>
export default {
  name: "Login",
  data() {
    return {
      storekeeper: {
        storekeeperName: "",
        password: "",
      },
      rules: {
        storekeeperName: [
          { required: true, message: "请输入用户名/手机号码", trigger: "blur" },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
    };
  },
  methods: {
    toStoreLogin() {
      window.location.href = process.env.USER_LOGIN_URL;
    },
    change() {
      // this.$emit("toRegister");
      this.$router.push("register");
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          //   alert("submit!");

          this.$http
            .post(
              process.env.URL_HEAD + "/store/storekeeper/login",
              {
                storekeeperName: this.storekeeper.storekeeperName,
                password: this.storekeeper.password,
              },
              { withCredentials: true }
            )
            .then(
              (response) => {
                console.log(response);

                if (response.data.status == "error") {
                  this.$alert("登录失败", "", {
                    confirmButtonText: "确定",
                  });
                } else if (response.data.status == "ok") {
                  console.log(
                    "登录成功！storekeeperId:",
                    response.data.storeKeeperId
                  );
                  // alert("登陆成功！")
                  // this.$cookies.set(
                  //   "storeKeeperId",
                  //   response.data.storeKeeperId
                  // );
                  // this.$cookies.set("storeKeeperName",this.storekeeper.storekeeperName);
                  //转移的页面
                  this.$router.push({
                    name: "index",
                    params: { storekeeperId: response.data.storeKeeperId },
                  });
                }
              },

              (response) => {
                this.$alert("系统异常，请重试" + response.data.message, "", {
                  confirmButtonText: "确定",
                });
              }
            );
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style scope>
.h1 {
  font-size: 50px;
}
.el-input {
  width: 200px;
}
</style>
